package com.job.common.upload;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.log4j.Logger;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author TaoYan
 */
public class FileUpload {

    private static Logger logger = Logger.getLogger(FileUpload.class);

//    private final static String END_POINT = "oss-cn-shanghai.aliyuncs.com";
    private final static String END_POINT = "oss-cn-beijing.aliyuncs.com";

//    private final static String ACCESS_KEY_ID = "LTAI8mkm1Gfdx9kK";
    private final static String ACCESS_KEY_ID = "LTAI4GEwgFH37brzB42siEBo";

//    private final static String ACCESS_KEY_SECRET = "h17mcv6cZs0zS1fVyFWQfPgT8CM0EM";
    private final static String ACCESS_KEY_SECRET = "HXyUWZJf8rHQQicLWZtTUEuEER6smi";

//    private final static String PICTURE_URL = "http://duanpanzi.oss-cn-shanghai.aliyuncs.com/";
    private final static String PICTURE_URL = "https://feixiayonggong.oss-cn-beijing.aliyuncs.com/";

    public static String upload(String url) {
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            InputStream inputStream = new URL(url).openStream();
            Date date = new Date();

            SimpleDateFormat format8 = new SimpleDateFormat("yyyyMMddHHmmsss", Locale.CHINA);
            String newFileName = format8.format(date) + (int) ((Math.random() * 9 + 1) * 2) + ".jpg";
            ossClient.putObject("feixiayonggong", newFileName, inputStream);
            return PICTURE_URL + newFileName;
        } catch (IOException e) {
            logger.error("upload", e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }

    public static String baseUpload(String bytes) {
        OSS ossClient = null;
        try {
            // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

            byte[] buffer = new BASE64Decoder().decodeBuffer(bytes);
            InputStream inputStream = new ByteArrayInputStream(buffer);
            // 上传Byte数组。
//            byte[] content = bytes.getBytes();
            Date date = new Date();

            SimpleDateFormat format8 = new SimpleDateFormat("yyyyMMddHHmmsss", Locale.CHINA);
            String newFileName = format8.format(date) + (int) ((Math.random() * 9 + 1) * 2) + ".jpg";
            ossClient.putObject("feixiayonggong", newFileName, inputStream);
            return PICTURE_URL + newFileName;
            // 关闭OSSClient。
        } catch (Exception e) {
            logger.error("upload", e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }

    public static String baseUploadPdf(String bytes) {
        OSS ossClient = null;
        try {
            // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

            byte[] buffer = new BASE64Decoder().decodeBuffer(bytes);
            InputStream inputStream = new ByteArrayInputStream(buffer);
            // 上传Byte数组。
//            byte[] content = bytes.getBytes();
            Date date = new Date();

            SimpleDateFormat format8 = new SimpleDateFormat("yyyyMMddHHmmsss", Locale.CHINA);
            String newFileName = format8.format(date) + (int) ((Math.random() * 9 + 1) * 2) + ".pdf";
            ossClient.putObject("feixiayonggong", newFileName, inputStream);
            return PICTURE_URL + newFileName;
            // 关闭OSSClient。
        } catch (Exception e) {
            logger.error("upload", e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }
    public static String excel(String bytes) {
        OSS ossClient = null;
        try {
            // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

            byte[] buffer = new BASE64Decoder().decodeBuffer(bytes);
            InputStream inputStream = new ByteArrayInputStream(buffer);
            // 上传Byte数组。
//            byte[] content = bytes.getBytes();
            Date date = new Date();

            SimpleDateFormat format8 = new SimpleDateFormat("yyyyMMddHHmmsss", Locale.CHINA);
            String newFileName = format8.format(date) + (int) ((Math.random() * 9 + 1) * 2) + ".xls";
            ossClient.putObject("feixiayonggong", newFileName, inputStream);
            return PICTURE_URL + newFileName;
            // 关闭OSSClient。
        } catch (Exception e) {
            logger.error("upload", e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }

    public static void main(String[] args) throws Exception{
        String s = upload("http://duanpanzi.oss-cn-shanghai.aliyuncs.com/2020061209150273.jpg");
        System.out.println(s);
    }
}
